package com.lboyang.common.dto;


import com.lboyang.common.enums.RpcResponseCodeEnum;
import lombok.*;

import java.io.Serializable;


/**
 * @Description; RPC 调用响应
 * @Date 2021/9/11 20:38
 * @author: A.iguodala
 */
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Builder
@ToString
public class NrpcResponse<T> implements Serializable {

    private static final long serialVersionUID = 715745410605631233L;

    /**
     * 对应的请求 ID
     */
    private String requestId;

    /**
     * 响应码
     */
    private Integer code;
    /**
     * 响应信息
     */
    private String message;
    /**
     * 响应实体数据
     */
    private T data;

    /**
     * 成功响应
     * @param data
     * @param requestId
     * @param <T>
     * @return
     */
    public static <T> NrpcResponse<T> success(T data, String requestId) {
        NrpcResponse<T> response = new NrpcResponse<>();
        response.setCode(RpcResponseCodeEnum.SUCCESS.getCode());
        response.setMessage(RpcResponseCodeEnum.SUCCESS.getMessage());
        response.setRequestId(requestId);
        if (null != data) {
            response.setData(data);
        }
        return response;
    }

    /**
     * 失败响应
     * @param rpcResponseCodeEnum
     * @param <T>
     * @return
     */
    public static <T> NrpcResponse<T> fail(RpcResponseCodeEnum rpcResponseCodeEnum) {
        NrpcResponse<T> response = new NrpcResponse<>();
        response.setCode(rpcResponseCodeEnum.getCode());
        response.setMessage(rpcResponseCodeEnum.getMessage());
        return response;
    }

}
